<template>
  <el-container>
    <el-aside width="250px">
      <!--  侧边栏必须写在Home，如果单独为组件会无法加载其中的数据，需手动刷新    -->
      <el-menu :default-active="this.$store.state.menus.editableTabsValue" background-color="#545c64"
               text-color="#fff"
               active-text-color="#ffd04b" class="el-menu-vertical-demo">
        <router-link to="/index">
          <el-menu-item index="Index" @click="selectMenu({name: 'Index',
                    title: '首页',path: '/index'})">
            <i class="el-icon-menu"></i>
            <span slot="title">首页</span>
          </el-menu-item>
        </router-link>

        <router-link :to="item.path" v-for="item in menuList" :key="item.name">
          <el-menu-item :index="item.name"
                        :key="item.name" @click="selectMenu(item)">
            <i class="el-icon-menu"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </router-link>


        <el-submenu :index="item.name" v-for="item in hasChildMenuList" :key="item.name">
          <template slot="title">
            <i :class="item.icon"></i>
            <span>{{ item.title }}</span>
          </template>
          <router-link :to="childMenu.path" v-for="childMenu in item.children" :key="childMenu.name">
            <el-menu-item :index="childMenu.name" @click="selectMenu(childMenu)">
              <template slot="title">
                <i :class="childMenu.icon"></i>
                <span slot="title">{{ childMenu.title }}</span>
              </template>
            </el-menu-item>
          </router-link>
        </el-submenu>

      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <page-header></page-header>
      </el-header>
      <el-main>
        <Tabs></Tabs>
        <div style="margin: 0 15px">
          <router-view/>

        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import PageHeader from "./inc/PageHeader";
import Tabs from "./inc/Tabs";

export default {
  name: "Home",
  components: {
    PageHeader, Tabs
  },
  methods: {
    selectMenu(item) {
      this.$store.commit("addTab", item);
    }
  },
  computed: {
    menuList: {
      get() {
        let navList = this.$store.state.menus.navList;
        let menus = [];
        navList.forEach(menu => {
          if (menu.type !== 0 || !menu.children) {
            menus.push(menu);
          }
        })
        return menus;
      }
    },
    hasChildMenuList: {
      get() {
        let navList = this.$store.state.menus.navList;
        let menus = [];
        navList.forEach(menu => {
          if (menu.children && menu.type === 0) {
            menus.push(menu);
          }
        })
        return menus;
      }
    }
  },
  created() {

  }

}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 20px;
}

.el-container {
  padding: 0;
  margin: 0;
  height: 100%;
}

.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  line-height: 200px;
}

.el-main {
  color: #333;
  padding: 2px 0 0 1px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-menu-vertical-demo {
  height: 100%;
}

a {
  text-decoration: none;
}
</style>
